package main

import (
	"encoding/hex"
	"time"
)

func main_test_file() {
	MyPrintf("\n ----main_test----- top:= \n ")
	test_temp_path_2()
	MyPrintf("\n -----main_test------- end:= \n ")
}

func test_temp_path() {
	tem := tempGetPath()
	MyPrintf("\n tempGetPath ===" + tem)
	tem2 := tempPath("rb_")
	MyPrintf("\n tempPath ===" + tem2)
}

func test_temp_path_2() {
	pathTemp := tempGetPath()
	mypath := pathTemp + "/robobloqgo/"
	MyPrintf("\n tempGetPath ===" + mypath)
	flagpath, error := PathExists(mypath)
	MyPrintf("\n PathExists tempGetPath === %v \n", flagpath)
	if flagpath == false {
		filePathCreat(mypath)
	}
	if error != nil {
		MyPrintf("\n tempGetPath === 检查目录出错 ")
		return
	}
	tempfile := mypath + "temp.hex"
	MyPrintf("\n tempGetPath ,tempfile ===" + tempfile)
	fw := fileUpWriteByString(tempfile, "fileSt---hello--")
	if fw == 0 {
		return
	}
	// copy path
	sourcepath := "/Users/user01/Downloads/mk18/code/go/src/rbserial/robobloqSerial/res/sources/mega"
	newpath := mypath + "mega"
	// flagpath, error = PathExists(newpath)
	// MyPrintf("\n PathExists tempGetPath === %v \n", flagpath)
	// if flagpath == false {
	// 	filePathCreat(newpath)
	// }
	fileCopyDir(sourcepath, newpath)

}

func testFileWrite() {
	data := "asadf \n fd22222 \n 33333 \n"
	fileN := "/Users/user01/Downloads/mk18/code/go/src/rbserial/robobloqSerial/res/build/mega99.ino"
	fileUpWriteByString(fileN, data)
}

func testIno() {
	//k1
	//comm := "/Applications/Arduino.app/Contents/MacOS/Arduino --board arduino:avr:mega:cpu=atmega2560 --port $port --upload $file/mega/mega.ino"
	//comm := "/Applications/Arduino.app/Contents/MacOS/Arduino --board arduino:avr:mega:cpu=atmega2560 --pref build.path=$dir/res/build/mega --port $port --upload $file/mega/mega.ino"
	//k2
	comm := "/Applications/Arduino.app/Contents/MacOS/Arduino --board arduino:avr:uno --pref build.path=$dir/res/build/uno --port $port --upload $file/uno/uno.ino"
	port := "/dev/tty.wchusbserial14230"
	serialUploadByArduinoCommCreat(port, comm)
	//serialUploadInoShell(port, co, false, nil, 1)
}

func testHex() {
	//k1
	cc := "$exe -C $config -v -p atmega2560 -cwiring -P $port -b 115200 -D -U flash:w:$hexk1.hex:i"
	//k2
	//cc := "$exe -C $config -v -p atmega328p -c arduino -P $port -b 115200 -D -U flash:w:$hexk2.hex:i"
	c2 := creatUpHexCommer(cc, "/dev/tty.wchusbserial14230")
	//MyPrintf(c2)
	ExecShell(c2)
	MyPrintf("\n\n ------------ 3:= \n ")
}

/*
	//ss := "766f69642073657475702829207b0a0a7d0a0a766f6964206c6f6f702829207b0a0a7d"
	//testMsgUpIno("", ss)
766f69642073657475702829207b0a0a7d0a0a766f6964206c6f6f702829207b0a0a7d
*/
func testMsgUpIno(part string, hexStr string) {
	MyPrintf("\n--------- testMsgUpIno ---1  \n")
	MyPrintf(hexStr)
	//buff := []byte(data)
	buff, err := hex.DecodeString(hexStr)
	if err != nil {
		return
	}
	st := string(buff[:])
	MyPrintf("\n--------- testMsgUpIno ---2  \n")
	MyPrintf(st)
	MyPrintf("\n--------- testMsgUpIno ---3  \n")
	///Users/user01/Downloads/mk18/code/go/src/rbserial/robobloqSerial/res/sources/mega
	fileN := "/Users/user01/Downloads/mk18/code/go/src/rbserial/robobloqSerial/res/build/mega99.ino"
	fileUpWriteByString(fileN, st)
	// db := serialBackData{}
	// db.port = ""
	// db.data = IntToString(1)
	// db.command = "se.upino"
	// messageSendToClient(db, nil)
	messageSendBackClient(Message{P: "", C: "se.upino", D: IntToString(1)}, nil, 1)
}

/*
制作服务器 生成hex
*/
func testInoCreatPath() {
	guid := GetGuid()
	//MyPrintf(guid)
	path := GetCurrentDirectory()
	p1 := path + "/res/sources/mega"
	p2 := path + "/res/build/" + guid
	p3 := p2 + "/mega"
	//check
	filePathCreat(p3)
	//copy
	fileCopyDir(p1, p3)

	// del
	// deleteFileOnDisk(p2)
}

func testInoBackHex_read() {
	//p1 := "/Users/user01/Downloads/mk18/code/go/src/rbserial/robobloqSerial/res/build/mega/mega.ino.hex"
	p1 := "/Users/user01/Downloads/mk18/code/go/src/rbserial/robobloqSerial/res/sources/mega/mega.ino"
	hexStr := fileRead2Hex(p1)
	MyPrintf("\n hex ------------- \n")
	MyPrintf(hexStr)
	MyPrintf("\n string ---------- \n")
	bytes, _ := hex.DecodeString(hexStr)
	MyPrintf(string(bytes))
}

/*
测试 返回 hex 文件；
*/
func testInoBackHex(port string, hexStr string, board string, comm string) {
	cc := "sv.hexback"
	buff, err := hex.DecodeString(hexStr)
	if err != nil {
		//messageSendToClient(serialBackData{port: "", data: "-401", command: cc}, nil)
		messageSendBackClient(Message{P: "", C: cc, D: "-401"}, nil, 1)
		return
	}
	path := GetCurrentDirectory()
	guid := GetGuid()
	p1 := path + "/res/sources/" + board
	p2 := path + "/res/build/" + guid //临时文件夹
	p3 := p2 + "/" + board            //源码
	p4 := p2 + "/b/"                  //生成的文件
	//creat path
	filePathCreat(p3)
	filePathCreat(p4)
	//copy
	fileCopyDir(p1, p3)

	// up file
	fileSt := string(buff[:])
	fileN := p3 + "/" + board + ".ino"
	fw := fileUpWriteByString(fileN, fileSt)
	//MyPrintf("\n fileN %s \n", fileN)
	//MyPrintf("\n fileST \n %s \n", fileSt)
	if fw == 0 {
		//messageSendToClient(serialBackData{port: "", data: "-402", command: cc}, nil)
		messageSendBackClient(Message{P: "", C: cc, D: "-402"}, nil, 1)
		deleteFileOnDisk(p2)
		return
	}
	exepath := "/Applications/Arduino.app/Contents/MacOS/Arduino"
	// build
	com2 := serialCreathexCommCreat(exepath, p4, fileN, board)
	//serialUploadInoShell(port, com2, true)
	rs := ExecShell(com2)
	if rs == 0 {
		deleteFileOnDisk(p2)
		rs = -404
	}
	hexPath := p4 + "/" + board + ".ino.hex"
	MyPrintf("\n %s \n \n", hexPath)
	hexStrBack := fileRead2Hex(hexPath)
	MyPrintf(hexStrBack)
	MyPrintf("\n \n")
	messageSendBackClient(Message{P: "", D: IntToString(rs), C: cc, O: hexStrBack}, nil, 1)
	if rs == 1 {
		time.Sleep(1100 * time.Millisecond)
		//deleteFileOnDisk(p2)
	}
}
